<?php
class AppController extends Controller {
	var $helpers = array('html','Form','javascript','Session');
	var $components = array('Auth','Session','RequestHandler');
	var $variables = array();
	
	function beforeFilter() {
		if ($_SERVER['REQUEST_URI']=="/asistencia/asistencias")
		{
			//$this->Auth->password('334b365232da322f265e9542708489c3bbc5faad');
			//$this->Auth->user('administrador');
			$this->Auth->fields = array('username' => 'administrador', 'password' => '334b365232da322f265e9542708489c3bbc5faad');
			//$this->Auth->login();
			//var_dump($_SERVER['REQUEST_URI']);Exit;
			//$this->Auth->loginRedirect = array('controller' => 'asistencias', 'action' => 'index');
			$this->Auth->userModel = 'Usuario';
			$this->Auth->userScope = array('Usuario.estado_id' =>1);
			$this->Auth->loginRedirect = array('controller' => 'menus', 'action' => 'index');
			$this->Auth->loginError = "El Usuario no se encuentra registrado, comuniquese con el administrador";
			$this->Auth->allow('login');
		}
		else
		{
			$this->Auth->userModel = 'Usuario';
			$this->Auth->userScope = array('Usuario.estado_id' =>1);
			$this->Auth->loginRedirect = array('controller' => 'menus', 'action' => 'index');
			$this->Auth->loginError = "El Usuario no se encuentra registrado, comuniquese con el administrador";
			$this->Auth->allow('login');
		}
		
		
		App::import('Vendor', 'JQGridPaginate', array('file' => 'JQGridPaginate' . DS . 'JQGridPaginate.php'));
		$this->JQGridPaginate = new JQGridPaginate();
		$this->jqGrid();
		
		App::import('Vendor', 'JQTreeGrid', array('file' => 'JQGridPaginate' . DS . 'JQTreeGrid.php'));
		$this->JQTreeGrid = new JQTreeGrid();
		
		app::import('Model',$this->modelClass); 

		$this->{$this->modelClass} = new $this->modelClass();

		$this->variables = $this->{$this->modelClass}->getModelConstantes($this->Auth->user());
	}
	
	function jqGrid(){
		/* 
		debug($this->modelClass);
		debug($this->{$this->modelClass}->belongsTo[$this->params['form']['q']]['foreignKey']]);
		debug($this->controllerClass);
		*/
		
		if($this->modelClass == 'Matricula'){
			$this->params['form'] = $this->params['url'];
		}
		if(!empty($this->params['form'])){
		    $this->JQGridPaginate->isGrid = isset($this->params['form']['nd']);
    		$this->JQGridPaginate->limit = $this->params['form']['rows'];
    		$this->JQGridPaginate->page = $this->params['form']['page'];
    		$this->JQGridPaginate->sidx = $this->params['form']['sidx'];
    		$this->JQGridPaginate->sord = $this->params['form']['sord'];
		
		    if(!empty($this->params['form']['q'])){
    			//Configure::write('debug',1);
    			$this->JQGridPaginate->modelopadre_id = array(
    				$this->modelClass.'.'.$this->{$this->modelClass}->belongsTo[$this->params['form']['q']]['foreignKey'] => $this->params['form']['modelopadre_id'],
    				$this->modelClass.'.estado_id' => 1
    			);
    			//debug($this->JQGridPaginate->modelopadre_id);
    		}
    		if($this->params['form']['_search'] == "false")
    			$this->JQGridPaginate->searchOn = false;
    		elseif ($this->params['form']['_search'] == "true")
    			$this->JQGridPaginate->searchOn = true;
    			
    		if($this->JQGridPaginate->searchOn){			
    			$this->JQGridPaginate->filters = $this->params['form']['filters'];	
    		}
		}
		

    		
	}
}
